import {
  observable,
  extendObservable,
  computed,
  toJS,
  action
} from '../../libs/mobx';
import {
  reqHeader,
  proxy
} from '../../utils/util.js';
import contestUtil from '../../utils/contestUtil.js';
import LoginStore from '../login/store.js';

var CurStore = function () {
  let me = this;
  let getDefault = function () {
    return {
      contestList:[],
      hasNext:true,
    }
  }
  let getDefaultListOptions = function () {
    return {
      photoListUrl: '',
      params: {
        access_token: LoginStore.userInfo.access_token || "",
        isWechat:true,
        page: 1,
        size: 10,
        type: 'json'
      }
    }
  }
  extendObservable(this, getDefault(), getDefaultListOptions());

  this.reset = action(() => {
    Object.assign(this, getDefault(), getDefaultListOptions());
  })

  this.setPhotoListUrl = action((str) => {
    this.reset();
    Object.assign(this, getDefaultListOptions(), {
      photoListUrl: str
    });
    this.loadData();
  })

  this.nextPage = action(() => {
    if (me.hasNext){
      this.params.page++;
      this.loadData();
    }
    
  })

  this.loadData = () =>{
    wx.request({
      url: me.photoListUrl,
      data: me.params,
      header: reqHeader,
      success: function (res) {
        if (res.statusCode != 200) {
          return;
        }
        res = res.data;
        if (res.status == 200) {

          if (!res.data.length || res.data.length < me.params.size) {
            me.hasNext = false;
          }
          for(let item of res.data){
            item.timeStr = contestUtil.getContestRTime(item,true);
          }
          
          me.contestList.push.apply(me.contestList, res.data);
          
        } else {
          console.log(res.message);
        }
      }
    });
  }
}

export default new CurStore();